import maps.map_temp as mtp
import maps.unit_temp as utp
from .color import *
import random


class Mirror(utp.baseUnit):
    def __init__(
        self,
        name: str,
        info: str,
        actions: list[str] = None,
        amount: int = 1,
        check_str: str = None,
        glass_str: str = None,
    ) -> None:
        super().__init__(name, info, actions, amount, check_str)
        self.glass_str = glass_str
        self.actions_dict["watch"] = self.watch

    def watch(self, args=None):
        if args:
            delta = random.randint(-50, -5)
            name = args["name"]
            return [
                (200, f"你看着镜子，忽然间，镜子中显出了一行字：{BLUE}{name}{NORMAL}"),
                (
                    "command",
                    f"data.setAttribute(addr=addr,attribute='hp',delta={delta})",
                ),
                (200, f"{RED}恍惚间，你感觉脚下一空，头晕目眩，在缓过神来已不知是何时。(hp{delta}){NORMAL}"),
            ]
        else:
            return {"name": "users[addr]['characterName']"}


class Grass(utp.IncrementalUnit):
    def __init__(
        self,
        name: str,
        info: str,
        actions: list[str] = None,
        amount: int = 3,
        check_str: str = None,
    ) -> None:
        super().__init__(
            name, info, actions, amount, check_str, delay=20, step=2, maximums=6
        )
        self.actions_dict["pick"] = self.pick

    def pick(self):
        result = self.amount - 1
        delta = random.randint(5, 50)
        if result < 0:
            return [(200, "还薅，草都被你薅秃了，等等再来看吧")]
        self.amount = result
        return [
            (200, f"你拔起了这株{self.name}"),
            (
                "command",
                f"data.setAttribute(addr=addr,attribute='hp',delta={delta})",
            ),
            ("command", f"commandLook(addr,message='look {self.name}')"),
            (200, f"不知怎的，也许是来自东方的神秘力量，你的气血恢复了一些(hp+{delta})"),
        ]


class World(mtp.WorldMap):
    def __init__(self) -> None:
        wooden_table = utp.baseUnit(
            name="木桌", info="一张普通的木桌。", check_str="桌上摆放着一个陶瓷花瓶，里面插着一朵白花。"
        )
        wooden_chair = utp.baseUnit(
            name="木凳",
            info="一张木制板凳，摆在桌旁。",
            amount=2,
            check_str="板凳上坐出来的凹痕清晰可见，看来是件老古董了。",
        )
        mirror = Mirror(name="镜子", info="一面镜子，你似乎忍不住去看它。")
        grass1 = Grass(name="杂草", info="一株杂草，没什么好看的。")
        grass2 = Grass(name="杂草", info="一株杂草，没什么好看的。")
        cj = mtp.Place(name="村长家", info="普通的江南民居，比村里那些布满青苔的精致了不少。")
        cj.addUnits([wooden_table, wooden_chair, mirror])
        h1 = mtp.Place(name="茅屋", info="低矮的茅草屋，看起来很不稳固。")
        h2 = mtp.Place(name="哨塔", info="用木头支起来的哨塔，负责看守村口大门，防止野狼侵入。")
        h3 = mtp.Place(name="粮仓", info="村子里的粮仓，里面堆着一些谷物，似乎有老鼠在吱吱叫。")
        h4 = mtp.Place(name="茅屋", info="低矮的茅草屋，看起来很不稳固。")
        f1 = mtp.Place(name=f"{GREEN}田地{NORMAL}", info="种着黄花菜的小片田地，微风荡漾，绿色的田野也掀起了波涛。")
        f2 = mtp.Place(name=f"{GREEN}田地{NORMAL}", info="种着黄花菜的小片田地，微风荡漾，绿色的田野也掀起了波涛。")
        f2.addUnit(grass1)
        f3 = mtp.Place(name=f"{GREEN}田地{NORMAL}", info="种着黄花菜的小片田地，微风荡漾，绿色的田野也掀起了波涛。")
        f3.addUnit(grass2)
        rd = mtp.Road(info="一段石板路，有的缝隙中还冒出了几株青苔。路边长着几朵白花，形似白鹭，十分新奇。")
        dm = mtp.Portal(name="东门", info="一个古老的石牌坊，精致的雕花早已被岁月抹平。上面的文字模糊不清，似乎是“鹭兰镇”。")
        dm.setDestination("trees")
        dm.setDestinationPosition([0, 2])
        wl = mtp.Wall(name=f"{GRAY}石墙{NORMAL}")
        self.world = [
            ["", "", "", "", "", rd, "", "", f1, f1, f1],
            ["", rd, rd, rd, rd, rd, "", "", f1, f1, f3],
            ["", "", "", "", h1, rd, rd, rd, f1, f1, f1],
            ["", "", "", "", "", rd, "", "", f2, f1, f1],
            [wl, wl, "", h4, "", rd, "", "", wl, wl, wl],
            [cj, rd, rd, rd, rd, rd, rd, rd, rd, rd, dm],
            [wl, wl, "", rd, h3, rd, "", h2, wl, rd, wl],
            ["", "", "", rd, "", rd, "", "", "", rd, ""],
            ["", "", "", rd, "", rd, "", "", "", rd, ""],
            [rd, rd, rd, rd, "", rd, "", "", "", rd, ""],
            ["", "", "", "", "", rd, "", "", "", "", ""],
        ]
        self.x_size = len(self.world[0])
        self.y_size = len(self.world)


world = World()

if __name__ in "__main__":
    Huub = World()
    print(Huub.formatWorldMap(ifAxis=True))
